﻿using Abp.Application.Services.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Yozeev.Dtos
{
    public class StatsPagedResultDto<T>: PagedResultDto<T> where T : class, new()
    {
        public T Total { get; set; }

        public Dictionary<string, IEnumerable<object>> Filters { get; set; }

        public StatsPagedResultDto(int count, List<T> t, Dictionary<string, IEnumerable<object>> filters) : base(count, t)
        {
            Filters = filters;
            BuildT(t);
        }

        public StatsPagedResultDto(int count, List<T> t):base(count, t) {
            BuildT(t);
        }

        private void BuildT(List<T> t)
        {
            T newT = new T();
            foreach (PropertyInfo p in newT.GetType().GetProperties())
            {
                if (p.PropertyType == typeof(long))
                {
                    p.SetValue(newT, t.Sum(r => (long)r.GetType().GetProperty(p.Name).GetValue(r)));
                }
                else if (p.PropertyType == typeof(decimal))
                {
                    p.SetValue(newT, t.Sum(r => (decimal)r.GetType().GetProperty(p.Name).GetValue(r)));
                }
                else if (p.PropertyType == typeof(int))
                {
                    p.SetValue(newT, t.Sum(r => (int)r.GetType().GetProperty(p.Name).GetValue(r)));
                }
            }
            Total = newT;
        }
    }
}
